package com.group7.mall.controller;

import com.group7.mall.bean.Result;
import com.group7.mall.bean.SimilarImageResult;
import com.group7.mall.dao.TbGoodsDao;
import com.group7.mall.dao.TbUserDao;
import com.group7.mall.entity.TbGoods;
import com.group7.mall.req.BaiduImageRequest;
import com.group7.mall.dto.GoodsPreviewDto;
import com.group7.mall.resp.SimilarImageResponse;
import com.group7.mall.service.UserGoodsService;
import com.group7.mall.util.BaiduSimilarImageServerUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("image-recognition")
public class ImageRecognitionController {

    @Resource
    private UserGoodsService userGoodsService;

    @Resource
    private TbGoodsDao tbGoodsDao;

    @PostMapping("add-image")
    public Result addImage(@RequestBody BaiduImageRequest baiduImageRequest) throws IOException {
        Result result = Result.ok();
//        String imageUrl = "http://117.72.40.8/images/test-info-image1.png";
        int i = BaiduSimilarImageServerUtils.addImage(baiduImageRequest);
        if (i == 216681) {
            result.setMessage("添加失败！图片已经存在库中，无法重复添加");
            result.setData(0);
        } else if (i == 200) {
            result.setMessage("添加成功！");
            result.setData(1);
        } else {
            result.setMessage("其它未知错误！百度云图库错误码为" + i);
            result.setData(-1);
        }
        return result;
    }

    @PostMapping("update-image-brief")
    public Result updateImageBrief(@RequestBody BaiduImageRequest baiduImageRequest) throws IOException {
        Result result = Result.ok();
//        String imageUrl = "http://117.72.40.8/images/test-info-image1.png";
        int i = BaiduSimilarImageServerUtils.updateImage(baiduImageRequest);
        if (i == 1) {
            result.setMessage("更新成功！");
            result.setData(1);
        } else if (i == -1) {
            result.setMessage("请求失败！");
            result.setData(-1);
        } else {
            result.setMessage("未知错误！百度云图库错误码为" + i);
            result.setData(0);
        }
        return result;
    }

    @PostMapping("search-similar-image-brief")
    public Result<SimilarImageResponse<SimilarImageResult>> searchSimilarImageBrief(@RequestBody BaiduImageRequest baiduImageRequest) throws IOException {
        SimilarImageResponse similarImageResponse = BaiduSimilarImageServerUtils.searchSimilarImageBrief(baiduImageRequest);
        List<SimilarImageResult> result = similarImageResponse.getResult();
        List<GoodsPreviewDto> goodsPreviewDtoList = new ArrayList<>();
        for (SimilarImageResult x: result) {
            goodsPreviewDtoList.add(userGoodsService.queryGoodsPreview(Integer.valueOf(x.getBrief())));
        }
        baiduImageRequest.setPageStart((baiduImageRequest.getPageNo() - 1) * baiduImageRequest.getPageSize());
        similarImageResponse.setPageSize((long) baiduImageRequest.getPageSize());
        similarImageResponse.setCurrent((long) baiduImageRequest.getPageNo());
        similarImageResponse.setRecords(goodsPreviewDtoList);
        TbGoods tbGoods = new TbGoods();
        tbGoods.setIsDeleted(0);
        similarImageResponse.setTotal(tbGoodsDao.count(tbGoods));
        return Result.ok(similarImageResponse);
    }
}
